ed8cc8
@@ -496,11 +496,12 @@
private OperationHandle executeStatementInternal(String statement,
       Map<String, String> confOverlay, boolean runAsync, long queryTimeout) throws HiveSQLException {
     acquire(true, true);
 
-    OperationManager operationManager = getOperationManager();
-    ExecuteStatementOperation operation = operationManager.newExecuteStatementOperation(
-        getSession(), statement, confOverlay, runAsync, queryTimeout);
-    OperationHandle opHandle = operation.getHandle();
+    ExecuteStatementOperation operation = null;
+    OperationHandle opHandle = null;
     try {
+      operation = getOperationManager().newExecuteStatementOperation(getSession(), statement,
+          confOverlay, runAsync, queryTimeout);
+      opHandle = operation.getHandle();
       operation.run();
       addOpHandle(opHandle);
       return opHandle;
@@ -508,10 +509,12 @@
private OperationHandle executeStatementInternal(String statement,
       // Refering to SQLOperation.java, there is no chance that a HiveSQLException throws and the
       // async background operation submits to thread pool successfully at the same time. So, Cleanup
       // opHandle directly when got HiveSQLException
-      operationManager.closeOperation(opHandle);
+      if (opHandle != null) {
+        getOperationManager().closeOperation(opHandle);
+      }
       throw e;
     } finally {
-      if (operation.getBackgroundHandle() == null) {
+      if (operation == null || operation.getBackgroundHandle() == null) {
         release(true, true); // Not async, or wasn't submitted for some reason (failure, etc.)
       } else {
         releaseBeforeOpLock(true); // Release, but keep the lock (if present).
